using RapidSpider.Views;
using System;
using System.Windows.Forms;
using System.Globalization;
using RapidSpider.AccountManagementAgents;

namespace RapidSpider.ApplicationControllers
{
    public partial class Controller
    {
        #region Methods
        private void SettingsView_Show(MainView theParentView)
        {
            // Instantiate an instance
            SettingsView theView = new SettingsView();

            theView.buttonDownloadFolder.Click +=
                new EventHandler(SettingsView_buttonDownloadFolder_Click);
            theView.buttonLogFileFolder.Click += 
                new EventHandler(SettingsView_buttonLogFileFolder_Click);
            theView.buttonCreateRapidshareComAccount.Click += 
                new EventHandler(SettingsView_buttonCreateRapidshareComAccount_Click);
            theView.buttonCopyRapidshareComPasswordToClipboard.Click +=
                new EventHandler(SettingsView_buttonCopyRapidshareComPasswordToClipboard_Click);
            theView.buttonExtendRapidshareComAccount.Click +=
                new EventHandler(SettingsView_buttonExtendRapidshareComAccount_Click);
            theView.buttonOK.Click += 
                new EventHandler(SettingsView_buttonOK_Click);
            theView.buttonCancel.Click += 
                new EventHandler(SettingsView_buttonCancel_Click);

            theView.checkboxAlwaysOnTop.Checked =
                RegistryAgent.AlwaysOnTop;

            theView.updownConcurrentDownloads.Value =
                RegistryAgent.ConcurrentDownloads;
            theView.updownBufferSize.Value =
                RegistryAgent.BufferSize;
            theView.updownTimeout.Value =
                RegistryAgent.Timeout;

            theView.textboxDownloadPath.Text =
                RegistryAgent.DownloadPath;
            theView.checkboxLogActions.Checked =
                RegistryAgent.LogActions;
            theView.textboxLogFilePath.Text =
                RegistryAgent.LogFilePath;

            #region Rapidshare.com
            theView.textboxRapidshareComPremiumAccountLogin.Text =
                RegistryAgent.RapidshareComLogin;
            theView.textboxRapidshareComPremiumAccountPassword.Text =
                RegistryAgent.RapidshareComPassword;
            theView.textRapidshareComPremiumAccountExpiry.Text =
                RegistryAgent.RapidshareComPremiumAccountExpiry;
            theView.checkboxCogent.Checked =
                RegistryAgent.RapidshareComCogent;
            theView.checkboxCogent2.Checked =
                RegistryAgent.RapidshareComCogent2;
            theView.checkboxDeutscheTelekom.Checked =
                RegistryAgent.RapidshareComDeutscheTelekom;
            theView.checkboxGlobalCrossing.Checked =
                RegistryAgent.RapidshareComGlobalCrossing;
            theView.checkboxGlobalCrossing2.Checked =
                RegistryAgent.RapidshareComGlobalCrossing2;
            theView.checkboxLevel3.Checked =
                RegistryAgent.RapidshareComLevel3;
            theView.checkboxLevel32.Checked =
                RegistryAgent.RapidshareComLevel32;
            theView.checkboxLevel33.Checked =
                RegistryAgent.RapidshareComLevel33;
            theView.checkboxLevel34.Checked =
                RegistryAgent.RapidshareComLevel34;
            theView.checkboxTeleglobe.Checked =
                RegistryAgent.RapidshareComTeleglobe;
            theView.checkboxTeliaSonera.Checked =
                RegistryAgent.RapidshareComTeliaSonera;
            theView.checkboxTeliaSonera2.Checked =
                RegistryAgent.RapidshareComTeliaSonera2;
            #endregion

            #region Hotfile.com
            theView.textboxHotfileComPremiumAccountLogin.Text =
                RegistryAgent.HotfileComLogin;
            theView.textboxHotfileComPremiumAccountPassword.Text =
                RegistryAgent.HotfileComPassword;
            #endregion

            // Show the form
            theView.ShowDialog(theParentView);
        }

        #region Event Handlers
        private void SettingsView_buttonLogFileFolder_Click(
            object sender,
            EventArgs e)
        {
            Application.DoEvents();

            Button theLogFileFolderButton = sender as Button;
            SettingsView theParentView =
                theLogFileFolderButton.FindForm() as SettingsView;

            switch (theParentView.FolderBrowserDialog.ShowDialog(theParentView))
            {
                case DialogResult.OK:
                    theParentView.textboxLogFilePath.Text =
                        theParentView.FolderBrowserDialog.SelectedPath;
                    break;
            }
        }
        
        private void SettingsView_buttonCreateRapidshareComAccount_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            Button theExtendAccountButton = sender as Button;
            SettingsView theParentView =
                theExtendAccountButton.FindForm() as SettingsView;

            RapidshareComAccountManagementAgent.CreateAccount(theParentView);
        }

        private void SettingsView_buttonExtendRapidshareComAccount_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            Button theExtendAccountButton = sender as Button;
            SettingsView theParentView =
                theExtendAccountButton.FindForm() as SettingsView;

            RapidshareComAccountManagementAgent.ExtendAccount(theParentView);
        }

        private void SettingsView_buttonCopyRapidshareComPasswordToClipboard_Click(
            object sender,
            EventArgs e)
        {
            Application.DoEvents();

            Button theCopyPasswordButton = sender as Button;
            SettingsView theParentView =
                theCopyPasswordButton.FindForm() as SettingsView;

            Clipboard.SetDataObject(
                theParentView.textboxRapidshareComPremiumAccountPassword.Text,
                true);

            MessageBox.Show(
                theParentView,
                "The password has been copied to the clipboard",
                theParentView.Text,
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button1);
        }

        private void SettingsView_buttonDownloadFolder_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            Button theDownloadFolderButton = sender as Button;
            SettingsView theParentView = 
                theDownloadFolderButton.FindForm() as SettingsView;

            switch (theParentView.FolderBrowserDialog.ShowDialog(theParentView))
            {
                case DialogResult.OK:
                    theParentView.textboxDownloadPath.Text =
                        theParentView.FolderBrowserDialog.SelectedPath;
                    break;
            }
        }

        private void SettingsView_buttonOK_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            Button theOKButton = sender as Button;
            SettingsView theParentView = theOKButton.FindForm() as SettingsView;

            this._MainView.TopMost =
                RegistryAgent.AlwaysOnTop =
                    theParentView.checkboxAlwaysOnTop.Checked;

            RegistryAgent.ConcurrentDownloads = Convert.ToInt32(
                theParentView.updownConcurrentDownloads.Value,
                CultureInfo.CurrentCulture);
            RegistryAgent.BufferSize = Convert.ToInt32(
                theParentView.updownBufferSize.Value,
                CultureInfo.CurrentCulture);
            RegistryAgent.Timeout = Convert.ToInt32(
                theParentView.updownTimeout.Value,
                CultureInfo.CurrentCulture);

            RegistryAgent.DownloadPath =
                theParentView.textboxDownloadPath.Text;
            RegistryAgent.LogActions =
                theParentView.checkboxLogActions.Checked;
            RegistryAgent.LogFilePath =
                theParentView.textboxLogFilePath.Text;

            #region Rapidshare.com
            RegistryAgent.RapidshareComLogin =
                theParentView.textboxRapidshareComPremiumAccountLogin.Text;
            RegistryAgent.RapidshareComPassword =
                theParentView.textboxRapidshareComPremiumAccountPassword.Text;
            RegistryAgent.RapidshareComCogent =
                theParentView.checkboxCogent.Checked;
            RegistryAgent.RapidshareComCogent2 =
                theParentView.checkboxCogent2.Checked;
            RegistryAgent.RapidshareComGlobalCrossing =
                theParentView.checkboxGlobalCrossing.Checked;
            RegistryAgent.RapidshareComGlobalCrossing2 =
                theParentView.checkboxGlobalCrossing2.Checked;
            RegistryAgent.RapidshareComLevel3 =
                theParentView.checkboxLevel3.Checked;
            RegistryAgent.RapidshareComLevel32 =
                theParentView.checkboxLevel32.Checked;
            RegistryAgent.RapidshareComLevel33 =
                theParentView.checkboxLevel33.Checked;
            RegistryAgent.RapidshareComLevel34 =
                theParentView.checkboxLevel34.Checked;
            RegistryAgent.RapidshareComTeleglobe =
                theParentView.checkboxTeleglobe.Checked;
            RegistryAgent.RapidshareComTeliaSonera =
                theParentView.checkboxTeliaSonera.Checked;
            RegistryAgent.RapidshareComTeliaSonera2 =
                theParentView.checkboxTeliaSonera2.Checked;
            #endregion

            #region Hotfile.com
            RegistryAgent.HotfileComLogin =
                theParentView.textboxHotfileComPremiumAccountLogin.Text;
            RegistryAgent.HotfileComPassword =
                theParentView.textboxHotfileComPremiumAccountPassword.Text;
            #endregion

            theParentView.Close();
        }

        private void SettingsView_buttonCancel_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            Button theCancelButton = sender as Button;
            SettingsView theParentView = theCancelButton.FindForm() as SettingsView;

            theParentView.Close();
        }
        #endregion
        #endregion
    }
}
